#include "downloadfunctiontablewidget.h"
#include <QHeaderView>

DownloadFunctionTableWidget::DownloadFunctionTableWidget(QWidget *parent) 
    : QTableWidget(parent)
{
    setFont(QFont("Helvetica"));
    setColumnCount(3);
    setShowGrid(false);//Does not display the grid
    QHeaderView *headerview = this->horizontalHeader();
    headerview->setVisible(false);
    headerview->resizeSection(0,45);
    headerview->resizeSection(1,25);
    headerview->resizeSection(2,110);
    verticalHeader()->setVisible(false);
    setMouseTracking(true);  //Open the capture mouse function
    setStyleSheet("QTableWidget{selection-background-color:rgba(0,0,0,150);}");
    //Set the color of selected row
    setFrameShape(QFrame::NoFrame);//Set No Border
    setEditTriggers(QTableWidget::NoEditTriggers);//No edit
    setSelectionBehavior(QTableWidget::SelectRows);
    //Multi-line election
    setSelectionMode(QAbstractItemView::SingleSelection);
    //Only select one row
    setTransparent(0);
    setFocusPolicy(Qt::NoFocus);

    m_previousColorRow = -1;
    m_defaultBkColor = QColor(255,255,255,0);

    connect(this,SIGNAL(cellEntered(int,int)),SLOT(listCellEntered(int,int)));

    addFunctionItems();
}

DownloadFunctionTableWidget::~DownloadFunctionTableWidget()
{

}

void DownloadFunctionTableWidget::setTransparent(int angle)
{
    QPalette pal = palette();
    pal.setBrush(QPalette::Base,QBrush(QColor(255,255,255,angle)));
    setPalette(pal);
}

void DownloadFunctionTableWidget::addFunctionItems()
{
    setRowCount(4);
    for(int i=0; i<rowCount(); ++i)
    {
        setItem(i,0,new QTableWidgetItem());

        QTableWidgetItem *item = new QTableWidgetItem(QIcon(":/image/newFile"),"");
        item->setTextAlignment(Qt::AlignCenter);
        setItem(i,1,item);

        QTableWidgetItem *item1 = new QTableWidgetItem(QString::number(231));
        item1->setTextColor(QColor(50,50,50));
        item1->setTextAlignment(Qt::AlignLeft | Qt::AlignVCenter);
        setItem(i,2,item1);
    }

}

void DownloadFunctionTableWidget::listCellEntered(int row, int column)
{
    QTableWidgetItem *it = item(m_previousColorRow, 0);
    if(it != NULL)
       setRowColor(m_previousColorRow, m_defaultBkColor);

    it = item(row, column);
    if(it != NULL && !it->isSelected() && !it->text().isEmpty())
       setRowColor(row, QColor(0,0,0,80));

    m_previousColorRow = row;
}

void DownloadFunctionTableWidget::setRowColor(int row,const QColor& color)
{
    for(int col=0; col<columnCount(); ++col)
    {
        QTableWidgetItem *it = item(row, col);
        it->setBackgroundColor(color);
    }
}
